package com.wannengbang.cloudleader.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.AgentListBean;
import com.wannengbang.cloudleader.homepage.AgentPerformanceActivity;
import com.wannengbang.cloudleader.utils.DateTimeUtil;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 合伙人列表
 */
public class ProviderListAdapter extends RecyclerView.Adapter<ProviderListAdapter.ViewHolder> {

    private List<AgentListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public ProviderListAdapter(List<AgentListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_service_provider, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AgentListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvName.setText(dataBean.getName());
        holder.tvMobile.setText("手机号：" + dataBean.getMobile());
        holder.tvCreateTime.setText("创建时间：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, AgentPerformanceActivity.class);
            intent.putExtra("name", dataBean.getName());
            intent.putExtra("agent_id", dataBean.getId() + "");
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_mobile)
        TextView tvMobile;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
